#if !ENABLE_INPUT_SYSTEM
using UnityEngine;
using UnityEngine.EventSystems;

namespace LS.SimpleInput
{
    public abstract class MouseButtonInputer : PointInputer
    {
        protected InputPhase m_Phase;
        protected float m_StartTime;
        protected float m_DeltaTime;
        protected float m_LastTime;
        protected Vector2 m_StartPoint;
        protected Vector2 m_Point;
        protected Vector2 m_DeltaPoint;

        public override InputPhase Phase => m_Phase;
        public override float StartTime => m_StartTime;
        public override float DeltaTime => m_DeltaTime;
        public override float LastTime => m_LastTime;
        public override float Duration => m_LastTime - m_StartTime;
        public override Vector2 StartPoint => m_StartPoint;
        public override Vector2 Point => m_Point;
        public override Vector2 DeltaPoint => m_DeltaPoint;
        public override int DeviceId => 0;
        public abstract int ButtonIndex { get; }

        public MouseButtonInputer(UserInputSetting setting) : base(setting)
        {
            
        }

        public MouseButtonInputer() : base()
        {
            
        }

        public override void Update()
        {
            if (UnityEngine.Input.GetMouseButtonDown(ButtonIndex))
            {
                m_Phase = InputPhase.Began;
                m_StartPoint = UnityEngine.Input.mousePosition;
                m_Point = m_StartPoint;
                m_DeltaPoint = Vector2.zero;
                m_StartTime = Time.unscaledTime;
                m_LastTime = Time.unscaledTime;
                m_DeltaTime = 0;
            }
            else if (UnityEngine.Input.GetMouseButtonUp(ButtonIndex))
            {
                m_Phase = InputPhase.Ended;
                Vector2 previousPosition = m_Point;
                m_Point = UnityEngine.Input.mousePosition;
                m_DeltaPoint = m_Point - previousPosition;
                float previousTime = m_LastTime;
                m_LastTime = Time.unscaledTime;
                m_DeltaTime = m_LastTime - previousTime;
            }
            else if (UnityEngine.Input.GetMouseButton(ButtonIndex))
            {
                Vector2 previousPosition = m_Point;
                m_Point = UnityEngine.Input.mousePosition;
                m_DeltaPoint = m_Point - previousPosition;
                float previousTime = m_LastTime;
                m_LastTime = Time.unscaledTime;
                m_DeltaTime = m_LastTime - previousTime;

                if ((m_Point - m_StartPoint).sqrMagnitude > Mathf.Pow(m_Setting.stationaryTolerance, 2))
                {
                    m_Phase = InputPhase.Moved;
                }
                else if (m_Phase != InputPhase.Moved)
                {
                    m_Phase = InputPhase.Stationary;
                }
            }
            else
            {
                m_StartPoint = Vector2.zero;
                m_Point = UnityEngine.Input.mousePosition;
                m_DeltaPoint = Vector2.zero;
                m_DeltaTime = 0;
                m_LastTime = 0;
                m_StartTime = 0;
                m_Phase = InputPhase.None;
            }
        }

    }
}
#endif
